/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package motiveai.tests;

import java.util.Iterator;
import motiveai.components.SensorEntity;
import motiveai.factors.Environment;

/**
 *
 * @author oxplay
 */
public class Test4Sensor extends SensorEntity<Test4Agent> {

    public Test4Sensor() {
    }

    @Override
    protected void examEnvironment(Test4Agent agent, Environment environment) {
        String description = "type: " + agent.agentType + " \n";
        description += "env left learning count: " + agent.getManager().learnCountLeft(environment) + " \n";
        description += "env hashcode: " + environment.getCalculatedHashCode() + " \n";
        description += "env isBored: " + environment.getBooleanFactor("isBored") + " \n";
        description += "env isRested: " + environment.getBooleanFactor("isRested") + " \n";
        description += "health: " + agent.health + " \n";
        description += "action: " + agent.actualBehaviour + " \n";
        description += "delay: " + (int) (agent.getAwakeTime() * 1000) + " \n";
        agent.thinking.setText(description);
        environment.setIntegerFactor("agentType", agent.agentType);
        environment.setBooleanFactor("inAttackRange", false);
        environment.setIntegerFactor("closeEnemyType", 0);
        agent.closeEnemy = null;
        Float minDistance = null;
        for (Iterator<Test4Agent> it = agent.getManager().getAgents().iterator(); it.hasNext();) {
            Test4Agent otherAgent = it.next();
            if (otherAgent.agentType != agent.agentType && otherAgent.health > 0) {
                float distance = agent.getSpatial().getLocalTranslation().distance(otherAgent.getSpatial().getLocalTranslation());
                if (minDistance == null || minDistance > distance) {
                    minDistance = distance;
                    if (distance < 50f) {
                        environment.setIntegerFactor("closeEnemyType", otherAgent.agentType);
                        agent.closeEnemy = otherAgent;
                        if (distance < 15f) {
                            environment.setBooleanFactor("inAttackRange", true);
                        }
                    }
                }
            }
        }
        agent.boringAddWait--;
        if (agent.closeEnemy == null && agent.boringAddWait <= 0) {
            agent.boringAddWait = 10;
            environment.setBooleanFactor("isBored", true);
        }
    }
}
